package cinescope2014.boundaries;

import cinescope2014.utilities.Globale;
import java.sql.*;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author pascal
 */
public class JIFMediaCRUD extends javax.swing.JInternalFrame {

    private Connection icn;
    private DefaultTableModel idtm;

    /**
     * Creates new form JIFMediaCRUD
     */
    public JIFMediaCRUD() {
        initComponents();

        setTitle("CRUD Media");

        try {
            // --- Connexion
//            Class.forName("org.gjt.mm.mysql.Driver");
//            String lsDSN = "jdbc:mysql://127.0.0.1:3306/cinescope2014";
//            icn = DriverManager.getConnection(lsDSN, "root", "");

            icn = Globale.getConnexion();
            idtm = (DefaultTableModel) jTableMedia.getModel();

            String lsSQL;
            PreparedStatement lpst;
            ResultSet lrs;

            lsSQL = "SELECT * FROM media ORDER BY nom_media";
            lsSQL = "{CALL mediaSelectAll()}";
            lpst = icn.prepareStatement(lsSQL);
            lrs = lpst.executeQuery();

            Object[] tLigne;

            while (lrs.next()) {
                tLigne = new Object[2];

                tLigne[0] = lrs.getString(1);
                tLigne[1] = lrs.getString(2);

                idtm.addRow(tLigne);
            }

            lrs.close();
            lpst.close();

            jLabelMessage.setText("Okay !!!");

        } catch (SQLException ex) {
            jLabelMessage.setText(ex.getMessage());
        }

        setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableMedia = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabelMessage = new javax.swing.JLabel();
        jTextFieldNom = new javax.swing.JTextField();
        jButtonAjouter = new javax.swing.JButton();
        jButtonSupprimer = new javax.swing.JButton();
        jButtonModifier = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabelID = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        jTableMedia.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Nom"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableMedia.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableMediaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTableMedia);

        jLabel2.setText("Nom");

        jLabelMessage.setText("Message");

        jTextFieldNom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldNomActionPerformed(evt);
            }
        });

        jButtonAjouter.setText("Ajouter");
        jButtonAjouter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAjouterActionPerformed(evt);
            }
        });

        jButtonSupprimer.setText("Supprimer");
        jButtonSupprimer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSupprimerActionPerformed(evt);
            }
        });

        jButtonModifier.setText("Modifier");
        jButtonModifier.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonModifierActionPerformed(evt);
            }
        });

        jLabel5.setText("ID");

        jLabelID.setForeground(new java.awt.Color(255, 0, 51));
        jLabelID.setText("ID");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane1)
                            .addComponent(jLabelMessage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel2))
                                .addGap(58, 58, 58)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextFieldNom, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabelID, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonAjouter)
                        .addGap(41, 41, 41)
                        .addComponent(jButtonModifier)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonSupprimer)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabelID))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldNom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonAjouter)
                    .addComponent(jButtonModifier)
                    .addComponent(jButtonSupprimer))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabelMessage)
                .addGap(10, 10, 10))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        //

//        try {
//            icn.close();
//        } catch (SQLException e) {
//            System.err.println(e.getMessage());
//        }
    }//GEN-LAST:event_formInternalFrameClosing

    private void jButtonAjouterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAjouterActionPerformed
        //
        if (jTextFieldNom.getText().trim().equals("")) {
            jLabelMessage.setText("Saisies obligatoires");
        } else {
            try {
                String lsSQL;
                lsSQL = "INSERT INTO media(id_media, nom_media) VALUES(?,?)";
                lsSQL = "{CALL mediaInsert(?,?)}";
                PreparedStatement lpst = icn.prepareStatement(lsSQL);

                lpst.setInt(1, Types.NULL);
                lpst.setString(2, jTextFieldNom.getText());

                lpst.executeUpdate();

                icn.commit();
                lpst.close();
                // Maj jtable
                String[] tLigne = new String[2];
                tLigne[0] = "";
                tLigne[1] = jTextFieldNom.getText();
                idtm.addRow(tLigne);
                jLabelMessage.setText("Okay !!!");
            } catch (SQLException e) {
                jLabelMessage.setText(e.getMessage());
                System.err.println(e.getMessage());
            }
        }
    }//GEN-LAST:event_jButtonAjouterActionPerformed

    private void jButtonModifierActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonModifierActionPerformed
        //
        int liLigneSelectionnee = jTableMedia.getSelectedRow();

        if (liLigneSelectionnee == -1) {
            jLabelMessage.setText("Sélectionnez une ligne !!!");
        } else {
            if (jTextFieldNom.getText().trim().equals("")) {
                jLabelMessage.setText("Saisies obligatoires");
            } else {
                try {
                    String lsSQL;
                    lsSQL = "UPDATE media SET nom_media=? WHERE id_media=?";
                    // mediaUpdate(paramID_MEDIA INT(10),paramNOM_MEDIA VARCHAR(254))
                    lsSQL = "{CALL mediaUpdate(?,?)}";

                    PreparedStatement lpst = icn.prepareStatement(lsSQL);

                    lpst.setString(1, jLabelID.getText());
                    lpst.setString(2, jTextFieldNom.getText());

                    lpst.executeUpdate();

                    icn.commit();
                    lpst.close();

                    jTableMedia.setValueAt(jTextFieldNom.getText(), liLigneSelectionnee, 1);

                    jLabelMessage.setText("Okay !!!");
                } catch (SQLException e) {
                    jLabelMessage.setText(e.getMessage());
                    System.err.println(e.getMessage());
                }
            }
        }
    }//GEN-LAST:event_jButtonModifierActionPerformed

    private void jButtonSupprimerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSupprimerActionPerformed
        //
        int liLigneSelectionnee = jTableMedia.getSelectedRow();
        String lsParam = "";

        if (liLigneSelectionnee != -1) {
            try {
                String lsSQL;
                if (jLabelID.getText().equals("")) {
                    lsSQL = "DELETE FROM media WHERE nom_media=?";
                    lsSQL = "{CALL media_delete_by_nom(?)}";
                    lsParam = jTextFieldNom.getText();
                } else {
                    lsSQL = "DELETE FROM media WHERE id_media=?";
                    lsSQL = "{CALL mediaDelete(?)}";
                    lsParam = jLabelID.getText();
                }

                PreparedStatement lpst = icn.prepareStatement(lsSQL);

                lpst.setString(1, lsParam);

                lpst.executeUpdate();

                icn.commit();
                lpst.close();

                idtm.removeRow(liLigneSelectionnee);

                jLabelID.setText("");
                jTextFieldNom.setText("");

                jLabelMessage.setText("Okay !!!");
            } catch (SQLException e) {
                jLabelMessage.setText(e.getMessage());
                System.err.println(e.getMessage());
            }
        } else {
            jLabelMessage.setText("Sélectionnez une ligne !!!");
        }
    }//GEN-LAST:event_jButtonSupprimerActionPerformed

    private void jTableMediaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableMediaMouseClicked
        //
        int liLigne = jTableMedia.getSelectedRow();

        String lsID = jTableMedia.getValueAt(liLigne, 0).toString();
        String lsNom = jTableMedia.getValueAt(liLigne, 1).toString();

        jLabelID.setText(lsID);
        jTextFieldNom.setText(lsNom);
    }//GEN-LAST:event_jTableMediaMouseClicked

    private void jTextFieldNomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldNomActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldNomActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAjouter;
    private javax.swing.JButton jButtonModifier;
    private javax.swing.JButton jButtonSupprimer;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabelID;
    private javax.swing.JLabel jLabelMessage;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableMedia;
    private javax.swing.JTextField jTextFieldNom;
    // End of variables declaration//GEN-END:variables
}
